// Package repository
// @Author: zhangdi
// @File: chat_details
// @Version: 1.0.0
// @Date: 2023/7/3 15:38
package repository

import (
	"errors"
	"github.com/fatih/structs"
	"gorm.io/gorm"
	"ideatools/application/db/dbdto"
	"ideatools/application/db/entity"
	"ideatools/application/db/sqllite"
	"ideatools/application/pkgs/utils"
	"ideatools/application/pkgs/where"

	"strconv"
)

type ChatContentRepository struct {
	db *gorm.DB
}

func NewChatContentRepository(db *gorm.DB) *ChatContentRepository {
	return &ChatContentRepository{db}
}

// Create 创建用户工具
func (r *ChatContentRepository) Create(dtoChatContent *dbdto.ChatContentAdd) (*entity.ChatContent, error) {
	//加锁
	sqllite.Mutex.Lock()
	defer sqllite.Mutex.Unlock()
	entityChatContent := new(entity.ChatContent)         //为分配一个内存空间
	utils.DtoToEntity(dtoChatContent, entityChatContent) //将dto转成entity
	result := r.db.Create(entityChatContent)
	return entityChatContent, result.Error
}

// FindOneByWhere 根据FieldValue查询一条数据：
func (r *ChatContentRepository) FindOneByWhere(wheres []*where.Field) (*entity.ChatContent, error) {
	var result *entity.ChatContent
	whereStr, whereValues, err := where.ConvertToGormWhere(wheres)
	if err != nil {
		return nil, err
	}
	if err := r.db.Where(whereStr, whereValues...).Find(&result).Error; err != nil {
		return nil, err
	}
	if result.ID < 1 {
		return nil, errors.New("查询结果为空，id=" + strconv.Itoa(int(result.ID)))
	}
	return result, nil
}

// FindList 根据FieldValue查询所有符合条件的数据列表：
func (r *ChatContentRepository) FindList(wheres []*where.Field) ([]*entity.ChatContent, error) {
	var resultList []*entity.ChatContent
	whereStr, whereValues, err := where.ConvertToGormWhere(wheres)
	if err != nil {
		return nil, err
	}
	if err := r.db.Where(whereStr, whereValues...).Find(&resultList).Error; err != nil {
		return nil, err
	}
	return resultList, nil
}

func (r *ChatContentRepository) FindListExtra(wheres []*where.Field, extra *where.Extra) ([]*entity.ChatContent, error) {
	var resultList []*entity.ChatContent
	tx := r.db

	//排序方式
	if extra.OrderByColumn != "" {
		if extra.OrderByDesc {
			tx = tx.Order(extra.OrderByColumn + " desc")
		} else {
			tx = tx.Order(extra.OrderByColumn + " asc")
		}
	}
	//limit
	if extra.Limit > 0 {
		tx = tx.Limit(extra.Limit)
	}

	//offset条件
	if extra.Offset > 0 {
		tx = tx.Offset(extra.Offset)
	} else {
		//取消offset条件
		tx = tx.Offset(-1)
	}

	//where 构造
	whereStr, whereValues, err := where.ConvertToGormWhere(wheres)
	if err != nil {
		return nil, err
	}

	if err := tx.Where(whereStr, whereValues...).Find(&resultList).Error; err != nil {
		return nil, err
	}
	return resultList, nil
}

func (r *ChatContentRepository) DeleteByFieldValue(wheres []*where.Field) error {
	//加锁
	sqllite.Mutex.Lock()
	defer sqllite.Mutex.Unlock()

	whereStr, whereValues, err := where.ConvertToGormWhere(wheres)
	if err != nil {
		return err
	}
	if err := r.db.Where(whereStr, whereValues...).Delete(&entity.ChatContent{}).Error; err != nil {
		return err
	}
	return nil
}

func (r *ChatContentRepository) Update(wheres []*where.Field, dtoChatContent *dbdto.ChatContentUpdate) error {
	//updateChatContentData := new(entity.ChatContent)         //为分配一个内存空间
	//utils.DtoToEntity(dtoChatContent, updateChatContentData) //将dto转成entity

	whereStr, whereValues, err := where.ConvertToGormWhere(wheres)
	if err != nil {
		return err
	}

	//转成map,不使用结构体，零值没办法更新
	updateDataMap := structs.Map(dtoChatContent)

	result := r.db.Model(entity.ChatContent{}).Where(whereStr, whereValues...).Updates(updateDataMap)

	if result.RowsAffected == 0 {
		return gorm.ErrRecordNotFound
	}

	return result.Error
}
